//
//  KFDEFTHoldingsViewController.m
//  KFD
//
//  Created by JIRENTIANXIANG on 6/24/15.
//  Copyright © 2015 kfd. All rights reserved.
//

#import "KFDETFHoldingsViewController.h"
#import "KFDETFHoldingsViewModel.h"
#import "KFDETFHoldingTableViewCell.h"
#import "ui_config.h"
#import "KFDEFTNavBarControl.h"
#import <MJRefresh/MJRefresh.h>

#define KFDETFHoldingTableViewCellReuseIdentifier @"KFDETFHoldingTableViewCell"
#define KFDETFHoldingTableViewCellNibName @"KFDETFHoldingTableViewCell"
#define KFDETFHoldingsTableViewHeaderNibName @"KFDETFHoldingsTableViewHeader"

@interface KFDETFHoldingsViewController ()

@property (strong,nonatomic) KFDETFHoldingsViewModel* viewModel;
@property (assign,nonatomic) NSInteger type;

@end

@implementation KFDETFHoldingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    KFDUIConfigurator* uiConfigurator=[KFDUIConfigurator configuratorForViewController:self];
    [uiConfigurator configureNavigationBarWithBackAction:@selector(goBack)];
    self.viewModel=[[KFDETFHoldingsViewModel alloc] init];
    self.type=1;
    KFDEFTNavBarControl* titleView=[[KFDEFTNavBarControl alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    [titleView setDidSelectTypeAction:^(NSInteger type) {
        self.type=type;
        if (type==1) {
            [self.viewModel loadGoldHoldingsWithMaxCount:20 page:1];
        }else if(type==2){
            [self.viewModel loadSilverHoldingsWithMaxCount:20 page:1];
        }
    }];
    self.navigationItem.titleView=titleView;
    [self addObserver];
    [self.viewModel loadGoldHoldingsWithMaxCount:20 page:1];
    [self setupRefreshing];
}

-(void)setupRefreshing
{
    __weak typeof(self) weakSelf=self;
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        if (self.type==1) {
            [weakSelf.viewModel loadGoldHoldingsWithMaxCount:20 page:1];
        }else if (self.type==2){
            [weakSelf.viewModel loadSilverHoldingsWithMaxCount:20 page:1];
        }
    }];
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        if (self.type==1) {
            [weakSelf.viewModel loadGoldHoldingsWithMaxCount:weakSelf.viewModel.goldHoldings.count+10 page:1];
        }else if (self.type==2){
            [weakSelf.viewModel loadSilverHoldingsWithMaxCount:weakSelf.viewModel.silverHoldings.count+10 page:1];
        }
    }];
}

-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)addObserver
{
    NSNotificationCenter* notificationCenter=[NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self selector:@selector(silverHoldingsReady) name:KFDSilverHoldingsReadyNotification object:nil];
    [notificationCenter addObserver:self selector:@selector(goldHoldingsReady) name:KFDGoldHoldingsReadyNotification object:nil];
}

-(void)dealloc
{
    NSNotificationCenter* notificationCenter=[NSNotificationCenter defaultCenter];
    [notificationCenter removeObserver:self name:KFDSilverHoldingsReadyNotification object:nil];
    [notificationCenter removeObserver:self name:KFDGoldHoldingsReadyNotification object:nil];
}

-(void)silverHoldingsReady
{
    dispatch_async(dispatch_get_main_queue(), ^{
        if ([self.tableView.header isRefreshing]) {
            [self.tableView.header endRefreshing];
        }
        if ([self.tableView.footer isRefreshing]) {
            [self.tableView.footer endRefreshing];
        }
        [self.tableView reloadData];
    });
}
-(void)goldHoldingsReady
{
    dispatch_async(dispatch_get_main_queue(), ^{
        if ([self.tableView.header isRefreshing]) {
            [self.tableView.header endRefreshing];
        }
        if ([self.tableView.footer isRefreshing]) {
            [self.tableView.footer endRefreshing];
        }
        [self.tableView reloadData];
    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

-(NSInteger)numberOfSectionsInTableView:(nonnull UITableView *)tableView
{
    return 1;
}

-(CGFloat)tableView:(nonnull UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 39.5;
}

-(UIView*)tableView:(nonnull UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* header=[[NSBundle mainBundle] loadNibNamed:KFDETFHoldingsTableViewHeaderNibName owner:self options:nil].firstObject;
    return header;
}

-(NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.type==1) {
        return self.viewModel.goldHoldings.count;
    }else if(self.type==2){
        return self.viewModel.silverHoldings.count;
    }
    return 0;
}

-(CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    return 32.5;
}

-(UITableViewCell*)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    KFDETFHoldingTableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:KFDETFHoldingTableViewCellReuseIdentifier];
    if (!cell) {
        NSArray *objects = [[NSBundle mainBundle] loadNibNamed:KFDETFHoldingTableViewCellNibName owner:nil options:nil];
        cell = objects[0];
    }
    if (self.type==1) {
        [cell setEFTHolding:self.viewModel.goldHoldings[indexPath.row]];
    }else if(self.type==2){
        [cell setEFTHolding:self.viewModel.silverHoldings[indexPath.row]];
    }
    return cell;
}

#pragma mark - Table view delegate

-(void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
